# @Time    : 2018/5/31 13:10
# @Author  : Jame
from db import models
from lib import common
from conf import setting
import os,logging





#1.通用的登陆接口
def login(name,password,role):
    logger = load_my_logging_cfg(role)

    if role == 'admin':
        obj = models.Admin.select(name)
    elif role == 'teacher':
        obj = models.Teacher.select(name)
    elif role == 'student':
        obj = models.Student.select(name)
    else:
        return False, 'error'
    if obj:
        if password == obj.password:
            logger.info('[%s] :%s 登陆成功!' % (role, name))

            return True, '[%s] :%s 登陆成功!' % (role, name)

        else:
            return False, '密码错误，请重试...'
    else:
        return False, '[%s]: %s  用户不存在，请重试...' % (role, name)






#2.选择校区接口，提供给管理员和学生公用
def chose_school():
    while True:
        print('请选择所在的校区:')
        files_list =common.get_files_name(os.path.join(setting.DB_DIR, 'school'))
        if not len(files_list)==0:
            for n, v in enumerate(files_list):
                print('%s : %s' % (n, v))
            school_num=input('请选择校区编号:').strip()
            if not school_num.isdigit():continue
            school_num=int(school_num)
            school_name=files_list[school_num]
            return True,school_name
        else:
            return False,'尚未创建校区，请创建校区后再使用该功能'







#3.选择课程，提供给老师和学生公用
def chose_course():
    while True:
        print('请选择课程:')
        files_list =common.get_files_name(os.path.join(setting.DB_DIR, 'course'))
        if not len(files_list)==0:
            for n, v in enumerate(files_list):
                print('%s : %s' % (n, v))
            course_num=input('请选择课程编号:').strip()
            if not course_num.isdigit():continue
            course_num=int(course_num)
            course_name=files_list[course_num]
            return True,course_name
        else:

            return False,'管理员尚未创建课程，请创建再使用该功能'





#4.记录日志的公共方法
def load_my_logging_cfg(role):
    logging.config.dictConfig(setting.LOGGING_DIC)  # 导入上面定义的logging配置
    logger = logging.getLogger(role)  # 生成一个log实例
    return logger


